package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.aop_whh.annotation.OperateLog;
import com.cskaoyan.wordmemorize.auth.SaAdminCheckPermission;
import com.cskaoyan.wordmemorize.auth.StpKit;
import com.cskaoyan.wordmemorize.common.constant.ResultCodeEnum;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.*;
import com.cskaoyan.wordmemorize.request.TopicCommand;
import com.cskaoyan.wordmemorize.request.VocPageRequest;
import com.cskaoyan.wordmemorize.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("admin")
public class TopicController {

    @Autowired
    TopicService topicService;

    @GetMapping("topic/singleChoice/list")
    public Result getTopicList(VocPageRequest request) {
        PageDTO<ReviewTopicDTO> singleChoicePage = topicService.getSingleChoicePage(request);
        return Result.ok(singleChoicePage);
    }

    @GetMapping("topic/fillWord/list")
    public Result getFillWordList(VocPageRequest request) {
        PageDTO<ReviewTopicDTO> fillWordPage = topicService.getFillWordPage(request);
        return Result.ok(fillWordPage);
    }

    //新增题目
    @OperateLog(dateType = "题目",operateType = 1)
    @PostMapping("topic")
    public Result postTopic(@RequestBody TopicCommand command) {
        topicService.addTopic(command);
        return Result.ok(ResultCodeEnum.SUCCESS);
    }

    //获取单个指定题目
    @GetMapping("topic")
    public Result getTopic(Long topicId) {
        ReviewTopicDTO topic = topicService.getTopic(topicId);
        return Result.ok(topic);
    }

    //删除题目
    @OperateLog(dateType = "题目",operateType = 2)
    @DeleteMapping("topic/{topicId}")
    public Result deleteTopic(@PathVariable Long topicId, Integer topicType) {
        topicService.deleteTopic(topicId, topicType);
        return Result.ok(ResultCodeEnum.SUCCESS);
    }


    @OperateLog(dateType = "题目选项",operateType = 2)
    @DeleteMapping("topic/option/{id}")
    public Result deleteTopicOption(@PathVariable Long id) {
        topicService.deleteTopicOption(id);
        return Result.ok(ResultCodeEnum.SUCCESS);
    }

    @GetMapping("topic/word")
    public Result getTopicWord(String word) {
        VocDTO vocDTO = topicService.checkWord(word);
        return Result.ok(vocDTO);
    }

    //没有测试成功，有报错
    @OperateLog(dateType = "题目topic",operateType = 2)
    @DeleteMapping("subtopic/{id}")
    public Result deleteSubtopic(@PathVariable Long id) {
        topicService.deleteSubTopic(id);
        return Result.ok();
    }

    //修改题目
    //前面有个报文报错，没有测试
    @OperateLog(dateType = "题目",operateType = 4)
    @PutMapping("topic")
    public Result putTopic(@RequestBody TopicCommand command) {
        topicService.updateTopic(command);
        return Result.ok();
    }
    // 检查excel中的题目(物料管理-题目管理)
    @PostMapping("/topic/excel/validation/{type}")
    public Result topicExcelVerify(@PathVariable("type")String type, @RequestParam("file") MultipartFile file) throws IOException {
        topicService.topicExcelValidate(type, file);
        return Result.ok();
    }
    // excel添加单选题目(物料管理-题目管理)
    @PostMapping("/topic/singleChoice")
    public Result uploadSingChoiceExcel(@RequestParam("file") MultipartFile file) throws IOException {
        Long employeeId = StpKit.ADMIN.getLoginIdAsLong();
        topicService.addSinleChoiceExcel(file, employeeId);
        return Result.ok();
    }
    // excel添加填空题目(物料管理-题目管理)
    @PostMapping("/topic/fillWord")
    public Result uploadFillWorldExcel(@RequestParam("file") MultipartFile file) throws IOException {
        Long employeeId = StpKit.ADMIN.getLoginIdAsLong();
        topicService.addFillWordExcel(file, employeeId);
        return Result.ok();
    }
}
